package com.yxsd.web.controller.admin;

import com.pandawork.core.exception.SSException;
import com.pandawork.core.log.LogClerk;
import com.pandawork.core.util.Pagination;
import com.yxsd.common.annotation.Module;
import com.yxsd.common.entity.Member;
import com.yxsd.common.entity.Order;
import com.yxsd.common.entity.Product;
import com.yxsd.common.enums.ModuleEnums;
import com.yxsd.common.enums.OrderStatusEnums;
import com.yxsd.common.utils.WebConstants;
import com.yxsd.web.spring.AbstractController;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * AdminOrderController
 *
 * @author: zhangteng
 * @time: 2015/1/6 13:59
 */
@Module(value = ModuleEnums.AdminOrder)
@Controller
@RequestMapping(value = "admin/order")
public class AdminOrderController extends AbstractController {

    @Module(value = ModuleEnums.AdminOrderList)
    @RequestMapping(value = {"", "list"}, method = RequestMethod.GET)
    public String list(@RequestParam(value = "pageNo", required = false) Integer pageNo,
                       @RequestParam(value = "search", required = false) String search,
                       Model model) {
        try {
            if (pageNo == null) {
                pageNo = 1;
            }
            if (search == null || search.equals("") || search.length() == 0) {
                search = null;
            }
            int dataCount = orderService.queryCountByCondition(search, null);
            Pagination page = new Pagination(dataCount, this.pageSize, pageNo);
            List<Order> orderList = orderService.queryByCondition(page, search, null);

            List<String> productList = new ArrayList<String>();
            List<String> memberList = new ArrayList<String>();
            if (orderList != null) {
                for (Order order : orderList) {
                    Product product = productService.queryById(Product.class, order.getProductId());
                    Member member = memberService.queryById(Member.class, order.getMemberId());
                    productList.add(product == null ? "" : product.getName());
                    memberList.add(member == null ? "" : member.getName());
                }
            }
            model.addAttribute("memberList", memberList);
            model.addAttribute("productList", productList);
            model.addAttribute("orderList", orderList);
            model.addAttribute("page", page);
            model.addAttribute("search", search);
            return "admin/order/list";
        } catch (SSException e) {
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
    }

    @Module(value = ModuleEnums.AdminOrderNew)
    @RequestMapping(value = "new", method = RequestMethod.GET)
    public String toInsert() {
        return "admin/order/new";
    }

    @Module(value = ModuleEnums.AdminOrderNew)
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String insert(Order order) {
        try {
            orderService.insert(order);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
        return "redirect:/admin/order";
    }

    @Module(value = ModuleEnums.AdminOrderEdit)
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String toUpdate(@PathVariable("id") Integer id,
                           Model model) {
        try {
            Order order = orderService.queryById(Order.class, id);
            model.addAttribute("order", order);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
        return "admin/order/edit";
    }

    @Module(value = ModuleEnums.AdminOrderEdit)
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public String update(Order order) {
        try {
            orderService.update(order);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
        return "redirect:/admin/order";
    }

    @RequestMapping(value = "del", method = RequestMethod.GET)
    public
    @ResponseBody
    JSONObject del(@RequestParam("id") Integer id) {
        try {
            Order order = new Order();
            order.setId(id);
            orderService.delete(order);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsgAndErrCode(e);
        }
        return sendSuccessMsg();
    }

    @Module(value = ModuleEnums.AdminOrderOrdered)
    @RequestMapping(value = "ordered/list", method = RequestMethod.GET)
    public String toOrderedList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
                                @RequestParam(value = "search", required = false) String search,
                                Model model) {
        try {
            if (pageNo == null) {
                pageNo = 1;
            }
            if (search == null || search.equals("") || search.length() == 0) {
                search = null;
            }
            int dataCount = orderService.queryCountByCondition(search, OrderStatusEnums.Ordered.getId());
            Pagination page = new Pagination(dataCount, this.pageSize, pageNo);
            List<Order> orderList = orderService.queryByCondition(page, search, OrderStatusEnums.Ordered.getId());

            List<String> productList = new ArrayList<String>();
            List<String> memberList = new ArrayList<String>();
            if (orderList != null) {
                for (Order order : orderList) {
                    Product product = productService.queryById(Product.class, order.getProductId());
                    Member member = memberService.queryById(Member.class, order.getMemberId());
                    productList.add(product==null ? "" : product.getName());
                    memberList.add(member==null ? "" : member.getName());
                }
            }
            model.addAttribute("memberList", memberList);
            model.addAttribute("productList", productList);
            model.addAttribute("orderList", orderList);
            model.addAttribute("page", page);
            model.addAttribute("search", search);
            return "admin/order/ordered_list";
        } catch (SSException e) {
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
    }

    @RequestMapping(value = "status/{id}/{status}", method = RequestMethod.GET)
    public @ResponseBody JSONObject updateStatus(@PathVariable("id") Integer id,
                                          @PathVariable("status") Integer status) {
        try {
            Order order = new Order();
            order.setId(id);
            order.setStatus(status);
            orderService.update(order);
        } catch (SSException e) {
            return sendErrMsgAndErrCode(e);
        }
        return sendSuccessMsg();
    }
}
